package components;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import org.dom4j.Element;
import org.dom4j.Node;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class AddPointDialog extends JDialog implements ItemListener {

	private final JPanel contentPanel = new JPanel();
	private HashMap<String, ArrayList<Pair<Double, Double>>> points = new HashMap<String, ArrayList<Pair<Double, Double>>>();
	private JTextField TF_x;
	private JTextField TF_y;
	private JComboBox<String> values, variables, pointsCombobox; 
	private JLabel lblPointHasBeen;

	JButton deletePointButton;
	private ArrayList<LinguisticVariable> variablesListTotal = new ArrayList<LinguisticVariable>(); // this is used to fetch all variables
	// and values inside those to the comboboxes
	ArrayList<LinguisticValue> valuetempList = new ArrayList<LinguisticValue>(); // values inside selected variable
	ArrayList<Pair<Double, Double>> pointtempList = new ArrayList<Pair<Double, Double>>(); // points inside selected value
	Double pointX, pointY; // these points are used whenever user wants to delete some points

	/**
	 * Create the dialog.
	 */
	public AddPointDialog(boolean isDeleteDialog,
			final ArrayList<LinguisticVariable> allVariables) {
		// This constructore generates the delete point Dialog Box
		values = new JComboBox(); // add values combobox to add the points to
		variables = new JComboBox(); // add the variable combobox to get the values from
		pointsCombobox = new JComboBox(); //this is for points 
		deletePointButton = new JButton("Delete"); // create a delete button to delete specific points
		variablesListTotal = allVariables; // get all variables currently in the list
		for (int index = 0; index < allVariables.size(); index++) { // get all variables into the combobox
			variables.addItem(allVariables.get(index).getName());

		}
		variables.setSelectedIndex(-1); // set no item selected
		variables.addItemListener(this); // add event so that when this is clicked it responds

		// root = variablesList;
		setBounds(100, 100, 450, 300); // sets the size and location of jdialog into the main screen with null layout
		getContentPane().setLayout(new BorderLayout()); // add borderlayou to main screen
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); // add border to main panel i.e content panel
		getContentPane().add(contentPanel, BorderLayout.CENTER); // add contentPanel to center of the dialog's screen
		contentPanel.setLayout(null); // set layout of dialog screen's contentPanel to null
		{
			values = new JComboBox(); // combobox to hold values
			values.setBounds(223, 40, 109, 27); // sets location and size of values combobox into the jdialog screen
			contentPanel.add(values); // add values to the main screen of jdialog
		}
		{
			JLabel lblLinguisticValue = new JLabel("Linguistic Variable:"); // label the variable combobox
			lblLinguisticValue.setBounds(94, 46, 130, 21); // sett label's location and size
			contentPanel.add(lblLinguisticValue);// add it to jdialog screen
		}
		{
			JLabel lblX = new JLabel("Values:"); // label the values combobox
			lblX.setBounds(183, 99, 80, 15);// set label's location and size
			contentPanel.add(lblX);// add label to main dialog's screen
		}
		{
			JLabel lblY = new JLabel("Points:"); // add Points Label infront of points textfield
			lblY.setBounds(183, 144, 80, 15); // sets the location and size of label
			contentPanel.add(lblY); // add label to screen of jdialog
		}
		variables.setBounds(223, 46, 109, 18); // set variable combobox location and size on dialog screen
		values.setBounds(223, 96, 109, 18);// set value combobox location and size on dialog screen
		pointsCombobox.setBounds(223, 141, 109, 18);// set points combobox location and size on dialog screen
		deletePointButton.setBounds(240, 182, 100, 20);// set deletePointButton's location and size on dialog screen
		deletePointButton.addActionListener(new ActionListener() {
			// add event to delete button so that when its clicked run the following code
			@Override
			public void actionPerformed(ActionEvent event) {
			
				int tempCount = 0;
				for (int variableIndex = 0; variableIndex < variablesListTotal
						.size(); variableIndex++) {
					if (variables
							.getSelectedItem()
							.toString()
							.equals(variablesListTotal.get(variableIndex)
									.getName())) {
						// check if the values prompted are correctly given by the user
						// get specific variable and bring all values to combobox
						// value, point and variable templists are created whenever the user selects specific variable
						// from the combobox
						for (int valueIndex = 0; valueIndex < valuetempList
								.size(); valueIndex++) {
							if (values
									.getSelectedItem()
									.toString()
									.equals(valuetempList.get(valueIndex)
											.getName())) {

								for (int pointIndex = 0; pointIndex < pointtempList
										.size(); pointIndex++) {
									if (pointtempList.remove(new Pair(pointX,
											pointY))) {
										// remove the chosen points inside of values
										JOptionPane.showMessageDialog(null,
												"Deleted Successfully");
										tempCount = 10; // means that the points were found and deleted form list of points
										break; // stop looping

									}

								}
								if (tempCount == 10) { //means the desired points were deleted no need to loop through rest of 
									// value points so stop looping
									break;
								}
							}

						}
						if (tempCount == 10) {//means the desired points were deleted no need to loop through rest of 
							// value points so stop looping
							break;
						}
					}

				}

			}
		});
		contentPanel.add(variables); // add variable combobox to jdialog screen
		contentPanel.add(values); // add value combobox to jdialog screen
		contentPanel.add(pointsCombobox); // add Points combobox to jdialog screen
		contentPanel.add(deletePointButton); // add delete to jdialog screen

		values.setSelectedIndex(-1); // set no item selected
		pointsCombobox.setSelectedIndex(-1); // sets no item selected

	}

	public AddPointDialog() {
		// this constructor creates Dialog to add point
		setBounds(100, 100, 450, 300); //set location and size of dialog boxl
		getContentPane().setLayout(new BorderLayout()); // set border layout
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));// adds border to contentpanel which is main area
		// inside the dialog box
		getContentPane().add(contentPanel, BorderLayout.CENTER);// align contentpanel at center in the dialog box
		contentPanel.setLayout(null); // set layout to null to easily arrange the components
		{
			values = new JComboBox(); // create combobox for values
			values.setBounds(223, 40, 109, 27);// set location and size 
			contentPanel.add(values); // add to dialog screen
		}
		{
			JLabel lblLinguisticValue = new JLabel("Linguistic Value:");// label the value texfield
			lblLinguisticValue.setBounds(94, 46, 119, 21); // set location and size
			contentPanel.add(lblLinguisticValue); // add to dialog screen
		}
		{
			JLabel lblX = new JLabel("X:"); // label X attribute of value
			lblX.setBounds(183, 99, 12, 15);// set location and size 
			contentPanel.add(lblX);// add lblX to dialog screen
		}
		{
			JLabel lblY = new JLabel("Y:"); // label Y attribute of value
			lblY.setBounds(183, 144, 12, 15);// set location and size 
			contentPanel.add(lblY);// add lblY to dialog screen
		}

		setX(new JTextField()); // add X attribute text field in jdialog screen
		getPointX().setBounds(223, 96, 109, 18); // sets location and size of X textField dialog box on screen
		contentPanel.add(getPointX()); // add to dialog box
		getPointX().setColumns(10); // set size

		setY(new JTextField());// add X attribute text field in jdialog screen
		getPointY().setBounds(223, 141, 109, 18);// sets location and size of Y textField dialog box on screen
		contentPanel.add(getPointY()); // add to dialog box
		getPointY().setColumns(10); // sets the size
		{
			lblPointHasBeen = new JLabel("Point has been added"); // success label in dialog box
			lblPointHasBeen.setVisible(false); // do not show it on screen for now
			lblPointHasBeen.setBounds(94, 204, 196, 15); // set location and size 
			contentPanel.add(lblPointHasBeen);// add to dialog screen
		}
		{
			JPanel buttonPane = new JPanel(); // creating panel to hold add and cancel buttons
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); //setting up layout
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Add"); // create add button
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// called when the add button is clicked
						Double x = Double.parseDouble(TF_x.getText()); // get value from X textField
						Double y = Double.parseDouble(TF_y.getText()); // get value from Y Textfield
						Pair<Double, Double> pair = new Pair<Double, Double>(x,
								y); // create a pait of X,Y to create a point
						if (!points.containsKey(getSelectedValue())) { // if this is not already containd in points list
							points.put(getSelectedValue(), 
									new ArrayList<Pair<Double, Double>>());
						}
						points.get(getSelectedValue()).add(pair);// add it to the points list
						lblPointHasBeen.setVisible(true); // show success label on screen
						JOptionPane.showMessageDialog(null, lblPointHasBeen.getText()); // show success messages on screen
						setVisible(false); // make JDialog disappear
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton); // add button to buttonPanel
				getRootPane().setDefaultButton(okButton); // set button to default
			}
			{
				JButton cancelButton = new JButton("Cancel"); // create cancel button
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// called when cancel button is clicked
						lblPointHasBeen.setVisible(false); // make success message disappear from dialog screen
						TF_x.setText(""); // remove X textField Contents
						TF_y.setText("");// remove X textField Contents
					}
				});
				cancelButton.setActionCommand("Cancel"); // Set text of cancel button
				buttonPane.add(cancelButton); // add it to button panel
			}
		}
	}

	public void setSelection(ArrayList<String> valueNames) {
		for (String name : valueNames) {
			values.addItem(name);
		}
		// values.setSelectedIndex(0);
	}

	public JTextField getPointX() {
		return TF_x;
	}

	public void setX(JTextField x) {
		this.TF_x = x;
	}

	public JTextField getPointY() {
		return TF_y;
	}

	public void setY(JTextField y) {
		this.TF_y = y;
	}

	private String getSelectedValue() {
		return (String) values.getSelectedItem();
	}

	public HashMap<String, ArrayList<Pair<Double, Double>>> getPoints() {
		return points;
	}

	@Override
	public void itemStateChanged(ItemEvent event) {
		// this event is called whenever we choose any specific variable from combobox or value combobox
		// if variable is chosen then fill up values combobox from the varibale list by getting all values of chosen
		// variable
		if (event.getSource().equals(pointsCombobox) // whenever event is fired on pointsCombobox
				&& pointsCombobox.getSelectedIndex() > -1
				&& ItemEvent.SELECTED == event.getStateChange()) {

		} else if (event.getSource().equals(variables)// whenever event is fired on valuesCombobox
				&& variables.getSelectedIndex() > -1
				&& ItemEvent.SELECTED == event.getStateChange()) {
			// whenever user chooses any variable the nget all values inside it and fill the value combobox on screen
			values.removeAllItems(); // remove all values 
			for (int index = 0; index < variablesListTotal.size(); index++) {
				if (variables.getSelectedItem().toString()
						.equals(variablesListTotal.get(index).getName())) {
					valuetempList = variablesListTotal.get(index)
							.getLinguisticValues();
					for (int valueIndex = 0; valueIndex < valuetempList.size(); valueIndex++) {
						values.addItem(valuetempList.get(valueIndex).getName()); // add value to value combobox

					}

				}

			}
			
			values.addItemListener(this);

		} else if (event.getSource().equals(values) // whenever event is fired on valuesCombobox
				&& values.getSelectedIndex() > -1
				&& ItemEvent.SELECTED == event.getStateChange()) {
			pointsCombobox.removeAllItems();
			// if user selects any specific value then fetch points inside it and fill points combobox
			for (int index = 0; index < valuetempList.size(); index++) {
				if (values.getSelectedItem().toString()
						.equals(valuetempList.get(index).getName())) {
					pointtempList = valuetempList.get(index).getPoints();
					for (int valueIndex = 0; valueIndex < pointtempList.size(); valueIndex++) {
						pointsCombobox
								.addItem(pointtempList.get(valueIndex).first
										+ ", "
										+ pointtempList.get(valueIndex).second); //adds points to points combobox from 
						//chosen value from values combobox
						pointX = pointtempList.get(valueIndex).first;
						pointY = pointtempList.get(valueIndex).second;
					}
				}

			}

		

		}

	}
}
